package com.explorati.design_pattern.structural.proxy.example2.cglibproxy;

import java.lang.reflect.Method;
import java.util.Date;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class GamePlayerCglibProxy implements MethodInterceptor{
	private Object target;
	public GamePlayerCglibProxy(Object target){
		this.target = target;
	}
	
//	public Object getProxy(){
//		Enhancer e = new Enhancer();
//		e.setSuperclass(target.getClass());
//		e.setCallback(this);
//		return e.create();
//	}

//	@Override
//	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
//		if ("killBoss".equals(method.getName())) {
//			this.log();
//			Object object = methodProxy.invoke(target, args);
//			return object;
//		}
//		if ("upGrade".equals(method.getName())) {
//			this.count();
//			Object object = methodProxy.invoke(target, args);
//			return object;
//		} else {
//			return null;
//		}
//	}
	
	@Override
	public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		if ("killBoss".equals(method.getName())) {
			this.log();
			Object object = methodProxy.invoke(target, null);
			return object;
		}
		if ("upGrade".equals(method.getName())) {
			this.count();
			Object object = methodProxy.invoke(target, args);
			return object;
		} else {
			return null;
		}
	}
	
	private void log() {
		System.out.println("打怪时间" + new Date().toString());
	}

	private void count() {
		System.out.println("升1级耗费50小时！");
	}

}
